<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '规格添加' : '规格编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="cancel"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="规格名称">
          <el-input v-model="form.specsname"></el-input>
        </el-form-item>
        <el-form-item
          label="规格属性"
          v-for="(item, index) in attrsArr"
          :key="index"
        >
          <div class="smBox">
            <el-input v-model="item.val"></el-input>
            <el-button @click="addAttr" v-if="index == 0"
              >新增规格属性</el-button
            >
            <el-button @click="delAttr(index)" v-else>删除</el-button>
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button v-if="obj.isAdd" @click="add()" type="primary"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="edit()">编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { specsAdd, specsEdit, specsInfo } from "../../../request/api";
import { successAlert, warningAlert } from "../../../utils/alert";
export default {
  props: ["obj"],
  data() {
    return {
      form: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      //将所有属性规格保存到数组中 渲染到页面用
      attrsArr: [{ val: "" }, { val: "" }],
    };
  },
  methods: {
    ...mapActions({
      asyncSpecsList: "specs/asyncChangeList",
      asyncSpecsCount: "specs/asynChangeCount",
      asyncSpecsPage: "specs/asyncChangePgae",
    }),
    cancel() {
      (this.form = {
        specsname: "",
        attrs: "",
        status: 1,
      }),
        (this.obj.isTrue = false),
        (this.attrsArr = [{ val: "" }, { val: "" }]);
    },
    add() {
      this.form.attrs = JSON.stringify(this.attrsArr.map((item) => item.val));
      specsAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          //重新请求列表数据
          this.asyncSpecsList();
          //   重新请求总条数
          this.asyncSpecsCount();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    addAttr() {
      this.attrsArr.push({ val: "" });
    },
    delAttr(index) {
      this.attrsArr.splice(index, 1);
    },
    getInfo(id) {
      specsInfo({ id: id }).then((res) => {
        this.form = {
          ...res.data.list[0],
          attrs: JSON.parse(res.data.list[0].attrs),
          id: id,
        };
        /* 
            我们想要的格式：[{val:红},{val:蓝},{val:绿}]
            form中attrs的格式：["红","蓝","绿"]
         */
        this.attrsArr = this.form.attrs.map((item) => {
          return { val: item };
        });
      });
    },
    edit() {
      //后台要的是字符串数组
      this.form.attrs = JSON.stringify(this.attrsArr.map((item) => item.val));
      specsEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.asyncSpecsList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  computed: {
    ...mapGetters({
      specsList: "specs/list",
      specsCount: "specs/count",
      specsSize: "specs/size",
    }),
  },
  mounted() {
    // this.asyncSpecsList();
  },
};
</script>

<style scoped>
.smBox {
  display: flex;
}
.smBox input {
  flex: 1;
}
</style>